//join_gender_list_ctrl
function join_gender_list_ctrl()
{
    $("#join_gender_list").show();
    return false;
}

function join_gender_list_elemt()
{
    $("#join_gender_name").val($(this).text());
    $("#join_gender").val($(this).attr("id"));
    $("#join_gender_list").hide();
}




function common_join()
{
    var user_type = 2;
    if ($("#common_user_type").attr("class") == "check-box check-false") {
	user_type = 2;
    }
    else {
	user_type = 1;
    }
    var email = $("#email").val();
    var password = $("#password").val();
    var paypal_email = $("#paypal_email").val();
    var firstname = $("#firstname").val();
    var lastname  = $("#lastname").val();
    var gender    = $("#join_gender").val();
    if (email == "" || password == "") {
	alert("email or password is empty.");
	return false;
    }
    if (is_email(email) == false) {
	alert("Email is not right.");	
	return false;
    }
    if (email.length > 64 || password.length > 64) {
	alert("email or password is too long.");
	return false;
    }
    if (paypal_email.length > 0 && is_email(paypal_email) == false) {
	alert("paypal email is not valid.");	
	return false;
    }
    if (is_name(firstname) == false) {
	alert("first name is not valid");
	return false;
    }
    if (is_name(lastname) == false) {
	alert("last name is not valid");
	return false;
    }
    var data = {
	email : email,
	password : password,
	paypal_email : paypal_email,
	firstname : firstname,
	lastname  : lastname,
	gender    : gender,
	user_type : user_type,
	area_id   : $("#area_id").val()
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/dojoin',
	success : function(msg){
	    if (msg.status == 0) {
		if (msg.data.has_pre_url) {
		    location.href="/user/basic";
		}
		else {
		    location.href="/user/basic";
		}
	    }
	    else if (msg.status == 1) {
		/**
        $("#pop3_face").attr("src", "/"+msg.data.thumb_photo);
		$("#pop3_username").text(msg.data.firstname + " " + msg.data.lastname);
		$("#pop3_firstname").text(msg.data.firstname); */
		$(".show_pop2").click();
	    }
        else if (msg.status == 3) {
            alert("Trainer can not post goal.");
            location.href="/user/dashboard";
        }
	    else {
		alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
}

function reset_password()
{
    var email = $("#forget_pwd_email").val();
    if (is_email(email) == false) {
        alert("Email is not valid.");
        return false;
    }
    var data={email:email};
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/forgetpwd',
	success : function(msg){
	    if (msg.status == 0) {
            alert("reset password success.");
            $.fancybox.close();
	    }
	    else {
            alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
}
function fb_getinfo_callback(response)
{
    var from_where = $("#from_where").val();
    var user_type = 2;
    if ($("#login_user_type").attr("class") == "check-box check-false" && $("#join_user_type").attr("class") == "check-box check-false") {
	user_type = 2;
    }
    else {
	user_type = 1;
    }

    $.cookie("user_type", user_type, {expires:30, path: '/'});
    var data = {
	fb_user_info : response,
	user_type    : user_type,
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/fblogin',
	success : function(msg){
	    if (msg.status == 0) {
                var html = '<a href="/user/dashboard" class="user-nickname">'+msg.data.firstname+'</a><a href="/user/logout">Logout</a>';
                $("#user-info").html(html);
		$("#logo").attr("href", "/user/dashboard");
                $.fancybox.close();
                if (location.href == "http://fitwow.com/#") {
                    location.href = "/user/dashboard";
                }
                return;
	    }
	    else if (msg.status == 1) {
		/* $("#pop3_face").attr("src", "/"+msg.data.thumb_photo);
		$("#pop3_username").text(msg.data.firstname + " " + msg.data.lastname);
		$("#pop3_firstname").text(msg.data.firstname); */
		$(".show_pop2").click();
	    }
        else if (msg.status == 2) {
            location.href="/user/basic";
        }
        else if (msg.status == 3) {
            alert("Trainer can not post goal.");
            location.href="/user/dashboard";
        }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
}

function fb_login_callback(response)
{
    $.cookie("access_token", response.authResponse.accessToken, {expires:30, path: '/'});
    if (response.status == "connected") {
	fb_getinfo(fb_getinfo_callback);
    }
}

function common_login()
{
    var email = $("#login_email").val();
    var password = $("#login_password").val();
    var from_where = $("#from_where").val();
    if (email == "" || password == "") {
	alert("email or password is empty.");
	return false;
    }
    if (is_email(email) == false) {
	alert("Email is not right.");	
	return false;
    }
    if (email.length > 64 || password.length > 64) {
	alert("email or password is too long.");
	return false;
    }
    var data = {
	email : email,
	password : password
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/user/commonlogin',
	success : function(msg){
	    if (msg.status == 0) {
		var html = '<a href="/user/dashboard" class="user-nickname">'+msg.data.firstname+'</a><a href="/user/logout">Logout</a>';
		$("#user-info").html(html);
		$("#logo").attr("href", "/user/dashboard");
		$.fancybox.close();
            if (location.href == "http://fitwow.com/#") {
                location.href = "/user/dashboard";
            }
		return;
	    }
	    else if (msg.status == 1) {
		/* $("#pop3_face").attr("src", "/"+msg.data.thumb_photo);
		$("#pop3_username").text(msg.data.firstname + " " + msg.data.lastname);
		$("#pop3_firstname").text(msg.data.firstname); */
		$(".show_pop2").click();
	    }
        else if (msg.status == 3) {
            alert("Trainer can not post goal.");
            location.href="/user/dashboard";
        }
	    else {
		alert(msg.content);
	    }

	},
	error : function(xhr) {
	    alert("Exception");
	}
    });
}

$(function(){	   
    $("#common_join").click(common_join);
    $("#common_login").click(common_login);

    $("#join_gender_list_ctrl").click(join_gender_list_ctrl);
    $("#join_gender_name").click(join_gender_list_ctrl);
    $("#join_gender_block").mouseleave(function(){$("#join_gender_list").hide();});
    $("#join_gender_list").find("li").each(function(){$(this).click(join_gender_list_elemt)});

    var user_type = $.cookie("user_type");
    if (user_type != null) {
	if (user_type == 1) {
	    $("#login_user_type").attr("class", "check-box check-true");
	    $("#join_user_type").attr("class", "check-box check-true");
	}
	else {
	    $("#login_user_type").attr("class", "check-box check-false");
	    $("#join_user_type").attr("class", "check-box check-false");
	}
    }
    $("#reset_password").click(reset_password);

    //for pop join or login context switch
    $("#login_sign_up_ctrl").fancybox({
	'width'			: '75%',
	'height'		: '75%',
	'autoScale'		: false,
	'transitionIn'		: 'none',
	'transitionOut'		: 'none'
    });
    $("#join_sign_in_ctrl").fancybox({
	'width'			: '75%',
	'height'		: '75%',
	'autoScale'		: false,
	'transitionIn'		: 'none',
	'transitionOut'		: 'none'
    });
    $(".header_pop").fancybox({
	'width'			: '75%',
	'height'		: '75%',
	'autoScale'		: false,
	'transitionIn'		: 'none',
	'transitionOut'		: 'none'
    });
    fb_init(document, fb_login_callback);
});
